package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.AdviceDao;
import com.hdaccp.xingmu.entity.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/jianyi")
public class AdviceConn {

    @Autowired
    private AdviceDao adviceDao;
    @PersistenceContext
    private EntityManager entityManager ;


    //跳转到建议意见这个页面
    @RequestMapping("/tiaozhuandaojianyi")
    public String tiaozhuan(){
        return "WEB-INF/jsp/shequfuwu/jianyiyijian";
    }
    //查询全部
    @RequestMapping("/quanbujianyi")
    @ResponseBody
    public Map<String ,Object> quanbujinayi(String firstDate,String secondDate,String zhuangtai,int pageSize, int pageNumber) {
        System.out.println(firstDate);
        System.out.println(secondDate);

        String jpal="SELECT a.id, a.order,a.num,a.date,a.yuangong,a.neirong,a.zhuangtai,i.name,a.shoulishijian  FROM Advice AS a \n" +
                "INNER JOIN Contenttype AS i ON a.contenttype = i.id WHERE 1=1";
            if (zhuangtai != null && !zhuangtai.equals("")) {
                jpal = jpal + " and zhuangtai=" + "'" + zhuangtai + "' ";
            }else if (firstDate!=null && !firstDate.equals("") && secondDate!=null && !secondDate.equals("") ){
                jpal=jpal+" and date>=" +"'" + secondDate +"' and date<=" +"'" + firstDate +"' ";
            }
        System.out.println(jpal);
        System.out.println(jpal);
        System.out.println(jpal);
        System.out.println(jpal);

            Query qu = entityManager.createQuery(jpal);
            //起始页书
            qu.setFirstResult((pageNumber - 1) * pageSize);
            //结束页数
            qu.setMaxResults(pageSize);

            //查询多少条数据
            String jpa = "select count(m) from Advice m where 1=1";
        if (zhuangtai != null && !zhuangtai.equals("")) {
            jpal = jpal + " and zhuangtai=" + "'" + zhuangtai + "' ";
        }else if (firstDate!=null && !firstDate.equals("") && secondDate!=null && !secondDate.equals("") ){
            jpal=jpal+" and date>=" +"'" + secondDate +"' and date<=" +"'" + firstDate +"' ";
        }
            Query q = entityManager.createQuery(jpa);

            Long count = (Long) q.getSingleResult();

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("total", count);
            map.put("rows", qu.getResultList());
            return map;


    }
    //添加建议信息
    @RequestMapping("/Qtianjia")
    @ResponseBody
    public Map<String ,Object> add2(Advice advice,int pageSize,int pageNumber){

        Advice advice1=new Advice();

        advice1.setOrder(advice.getOrder());
        advice1.setNum(advice.getNum());
        advice1.setShoulishijian(advice.getShoulishijian());

        advice1.setDate(advice.getDate());
        advice1.setYuangong(advice.getYuangong());


        advice1.setZhuangtai(advice.getZhuangtai());
        advice1.setNeirong(advice.getNeirong());
        advice1.setContenttype(advice.getContenttype());


        adviceDao.save(advice1);
        return quanbujinayi(null,null,null,pageSize,pageNumber);
    }
    //修改数据
    @RequestMapping("Qxiugai")
    @ResponseBody
    public Map<String ,Object> Qupd(String num,int id,int contenttype,String date,String yuangong,String neirong,String zhuangtai,String shoulishijian,String order,int pageNumber,int pageSize){
        Advice advice1=new Advice();
        advice1.setId(id);
        advice1.setNum(num);
        advice1.setOrder(order);
        advice1.setContenttype(contenttype);
        advice1.setDate(Date.valueOf(date));
        advice1.setYuangong(yuangong);
        advice1.setNeirong(neirong);
        advice1.setZhuangtai(zhuangtai);
        advice1.setShoulishijian(Date.valueOf(shoulishijian));
        Advice advice2 = adviceDao.save(advice1);

        return quanbujinayi(null,null,null,pageSize,pageNumber);

    }
    //删除数据
    @RequestMapping("/Qdel")
    @ResponseBody
    public Map<String ,Object> Qdel(long id,int pageSize,int pageNumber){
        adviceDao.deleteById(id);
        return quanbujinayi(null,null,null,pageSize,pageNumber);
    }



}
